package com.healthdata.service.impl;

import com.healthdata.dao.HospitalKindMapper;
import com.healthdata.dao.MedicalInstitutionsRegionYearMapper;
import com.healthdata.entity.HospitalKind;
import com.healthdata.service.HospitalKindService;
import com.healthdata.vo.HospitalKindVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class HospitalKindServiceImpl implements HospitalKindService {

    @Autowired
    private HospitalKindMapper hospitalmapper;

    @Autowired
    private MedicalInstitutionsRegionYearMapper medicalInstitutionsRegionYearMapper;

    //根据id找到一条数据
    public HospitalKind getByRegionYearId(Integer regionYearId) {
        return hospitalmapper.selectById(regionYearId);
    }


    // 获取某个省份连续五年的各类医院的数量
    public HospitalKindVO getFiveYearsHospitalData(String region, int startYear) {
        HospitalKindVO hospitalKindVO = new HospitalKindVO();
        List<List<Object>> source = new ArrayList<>();

        // 构建表头
        List<Object> header = new ArrayList<>();
        header.add("hospitalType");
        for (int i = 0; i < 5; i++) {
            header.add(startYear + i);
        }
        source.add(header);

        // 构建各类医院的数据
        List<Object> comprehensiveList = new ArrayList<>();
        comprehensiveList.add("综合医院");
        List<Object> chineseList = new ArrayList<>();
        chineseList.add("中医院");
        List<Object> integratedList = new ArrayList<>();
        integratedList.add("中西医结合医院");
        List<Object> ethnicList = new ArrayList<>();
        ethnicList.add("民族医院");
        List<Object> specializedList = new ArrayList<>();
        specializedList.add("专科医院");
        List<Object> nursingList = new ArrayList<>();
        nursingList.add("护理院");

        for (int i = 0; i < 5; i++) {
            int year = startYear + i;
            // 根据省份和年份获取id
            int id = medicalInstitutionsRegionYearMapper.getIdByRegionAndYear(region, year);
            // 根据id获取医院数据
            HospitalKind hospitalKind = hospitalmapper.selectById(id);
            if (hospitalKind != null) {
                comprehensiveList.add(hospitalKind.getComprehensive());
                chineseList.add(hospitalKind.getChinese());
                integratedList.add(hospitalKind.getIntegrated());
                ethnicList.add(hospitalKind.getEthnic());
                specializedList.add(hospitalKind.getSpecialized());
                nursingList.add(hospitalKind.getNursing());
            }
        }

        source.add(comprehensiveList);
        source.add(chineseList);
        source.add(integratedList);
        source.add(ethnicList);
        source.add(specializedList);
        source.add(nursingList);

        hospitalKindVO.setSource(source);
        return hospitalKindVO;
    }
}
